package com.cmcc.aregister.ui.activity;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.cmcc.aregister.R;
import com.cmcc.aregister.cs.constant.ConfigUtilHelper;

public class One39WebsiteLoading extends ParentActivity implements
    OnClickListener {
    private final String TAG = "One39WebsiteLoading";

    private WebView webview;
    private ProgressDialog progressDialog;
    private TextView refreshBtn;
    Thread thread;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.one39_website_view);
        refreshBtn = (TextView) findViewById(R.id.refresh_btn);
        refreshBtn.setOnClickListener(this);
        webview = (WebView) findViewById(R.id.website_view);
        loadUrl();
    }

    private void loadUrl() {
        progressDialog = new ProgressDialog(this);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setTitle(getString(R.string.notice));
        progressDialog.setIcon(android.R.drawable.ic_dialog_info);
        progressDialog.setMessage(getString(R.string.loading_web));
        progressDialog.setIndeterminate(true);
        progressDialog.setCancelable(true);
        progressDialog.show();

        thread = new Thread(new Runnable() {
            @Override
            public void run() {
                webview.getSettings().setJavaScriptEnabled(true);
                webview.getSettings().setPluginsEnabled(true);
                webview.getSettings().setSupportZoom(true);
                // webview.getSettings().setBuiltInZoomControls(true);
                // 加载url前，设置图片阻塞
                // webview.getSettings().setBlockNetworkImage( true );
                webview.loadUrl(ConfigUtilHelper.wuxianchengshi);
                MyWebViewClient myWebView = new MyWebViewClient();
                webview.setWebViewClient(myWebView);
            }
        });
        thread.start();

    }

    @Override
    // 设置回退
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && webview != null
            && webview.canGoBack()) {
            webview.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            // 加载完毕后，关闭图片阻塞
            webview.getSettings().setBlockNetworkImage(false);
            progressDialog.dismiss();
        }

        @Override
        public void onReceivedError(WebView view, int errorCode,
            String description, String failingUrl) {
            // TODO Auto-generated method stub
            super.onReceivedError(view, errorCode, description, failingUrl);
            progressDialog.dismiss();
        }
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        if (ConfigUtilHelper.debug) {
            Log.i(TAG, "------onDestroy------");
        }
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        if (ConfigUtilHelper.debug) {
            Log.i(TAG, "------onPause------");
        }
    }

    @Override
    protected void onRestart() {
        // TODO Auto-generated method stub
        super.onRestart();
        if (ConfigUtilHelper.debug) {
            Log.i(TAG, "------onRestart------");
        }
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        if (ConfigUtilHelper.debug) {
            Log.i(TAG, "------onResume------");
        }
    }

    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        if (ConfigUtilHelper.debug) {
            Log.i(TAG, "------onStart------");
        }
    }

    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
        if (ConfigUtilHelper.debug) {
            Log.i(TAG, "------onStop------");
        }
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if (v == refreshBtn) {
            loadUrl();
        }
    }

}
